package patrol.mobile.com.mobilepatrol.manager;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.bean.PatrolDetailInfoBean;
import patrol.mobile.com.mobilepatrol.bean.PatrolInfoBean;

import static patrol.mobile.com.mobilepatrol.PatrolApp.realm;

/**
 * Created by apple on 17/11/6.
 */

public class TrackManager {
    /**
     * 获取track列表
     */
    public static List<PatrolInfoBean> getAllList(){
        // 1
        final RealmResults<PatrolInfoBean> results =  PatrolApp.realm.where(PatrolInfoBean.class).findAll();
        // 2
        results.sort("roadDate", Sort.DESCENDING);
        // 3
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<results.size();i++){
                    results.get(i).index = i+1;
                }

            }
        });
        return results;
    }
    /**
     * 获取一个对象的track detail列表
     */
    public static List<PatrolDetailInfoBean> getAllDetailList(String ID){
        final RealmResults<PatrolDetailInfoBean> results =  PatrolApp.realm.where(PatrolDetailInfoBean.class).equalTo("patrolID",ID).findAll();
        results.sort("checkDate", Sort.DESCENDING);
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (int i=0;i<results.size();i++){
                    results.get(i).index = i+1;
                }

            }
        });
        return results;
    }
    /**
     * 获取一个对象的track detail列表
     */
    public static int getAllDetailCounts(String ID){
        RealmResults<PatrolDetailInfoBean> results =  PatrolApp.realm.where(PatrolDetailInfoBean.class).equalTo("patrolID",ID).findAll();
        if (results!=null&&results.size()>0){
            return results.size();
        }else{
            return 0;
        }

    }
    /**
     * 获取一个对象的track detail对象
     */
    public static PatrolDetailInfoBean getAllDetailBean(String ID){
        RealmResults<PatrolDetailInfoBean> results =  PatrolApp.realm.where(PatrolDetailInfoBean.class).equalTo("ID",ID).findAll();
        return results.get(0);
    }
    /**
     * 根据id获取一个对象
     */
    public static PatrolInfoBean getPatrol(String ID){
        RealmResults<PatrolInfoBean> results =  PatrolApp.realm.where(PatrolInfoBean.class).equalTo("ID",ID).findAll();
        return results.get(0);
    }
    /**
     * 根据id删除一个对象
     */
    public static void delPatrol(final String ID){
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<PatrolInfoBean> results = realm.where(PatrolInfoBean.class).equalTo("ID",ID).findAll();
                RealmResults<PatrolDetailInfoBean> details = realm.where(PatrolDetailInfoBean.class).equalTo("patrolID",ID).findAll();
                details.deleteAllFromRealm();
                results.deleteAllFromRealm();

            }
        });

    }
    /**
     * 根据id删除巡道明细一个对象
     */
    public static void delPatrolDetail(final String ID){
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<PatrolDetailInfoBean> details = realm.where(PatrolDetailInfoBean.class).equalTo("ID",ID).findAll();
                details.deleteAllFromRealm();
            }
        });

    }
    /**
     * 增加
     */
    public static void insertT(PatrolInfoBean p){
        realm.beginTransaction();
        realm.insert(p);
        realm.commitTransaction();
    }
    /**
     * 设置list的上传状态
     */
    public static void setListUpState(final int status){
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                List<PatrolInfoBean> mlist = realm.where(PatrolInfoBean.class).findAll();
                for (int i=0;i<mlist.size();i++){
                    mlist.get(i).status = status;
                }
            }
        });
    }


}
